package gwtadmin.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;

public class FormUpload extends VerticalPanel implements ClickHandler,ChangeHandler {
	
	public FormUpload(IWsdlManager wsdlManager) {

//		wsdlMan = wsdlManager;
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(btnUpload);
		hp.add(btnCancel);
		
		Grid grid = new Grid(3, 2);
		grid.setWidget(0, 0, new Label("WSDL"));
		grid.setWidget(0, 1, upload);
		grid.setWidget(1, 0, new Label("Name"));
		grid.setWidget(1, 1, name);
		grid.setWidget(2, 1, hp);
		grid.addStyleName("decorFrame");
		grid.getElement().setAttribute("style", "padding: 20px");
		
		form.setWidget(grid);
		form.setAction("mywebadmin/updo");
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
	
//		name.setName("name");
		name.getElement().setId("editName");
		upload.getElement().setId("editUpload");
		upload.setName("wsdl");
		upload.getElement().setAttribute("onChange", "UpdateFileName()");
		
		form.addSubmitHandler(new FormPanel.SubmitHandler() {
			@Override
			public void onSubmit(SubmitEvent event) {
				if (upload.getFilename().equals("")) {
					event.cancel();
					Window.alert("You forget select file");
					return;
				}
				
				if (name.getText().equals("")) {
//					event.cancel();
					String str = ExtractFileName(upload.getFilename());
//					Window.alert("You forget enter name of WSDL. Using " + str);
					upload.setName(str);
				} else {
					upload.setName(name.getText());
				}
			}
		});
		
		form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				form.reset();
//				Window.alert("File is successfuly uploaded!");
			}
		});
		
		add(form);
	}
	
	public void onClick(ClickEvent event) {
		
		Button btn = (Button)event.getSource();
		
		if (btn == btnUpload) {
			form.submit();
		} else if (btn == btnCancel) {
			form.reset();
		}
	}
	
	private String ExtractFileName(String fileName) {
		int ind = fileName.lastIndexOf('\\');
		if (ind == -1)
			ind = fileName.lastIndexOf('/');
		
		return fileName.substring(ind+1);
	}
	
	private FormPanel form = new FormPanel();
	private FileUpload upload = new FileUpload();
	private TextBox name = new TextBox();

	private Button btnUpload = new Button("Upload", this);
	private Button btnCancel = new Button("Reset", this);
	
//	private IWsdlManager wsdlMan = null;

	@Override
	public void onChange(ChangeEvent event) {
		Window.alert("asdsad");
		if (!upload.getFilename().equals("")) {
			int ind = upload.getFilename().lastIndexOf('\\');
			if (ind == -1)
				ind = upload.getFilename().lastIndexOf('/');
//			if (ind == -1)
			
			String fileName = upload.getFilename().substring(ind+1);
			name.setText(fileName);
		}
	}
}
